Page({
    data: {
      // 公告信息
      showAnnouncement: true,
      announcementText: "通知：请各位同学及时认领本学期遗失的校园卡，截止日期为本月月底",
      
      // 搜索相关
      searchValue: "",
      
      // 分类标签
      activeTab: 0,
      tabTitles: ["全部", "电子设备", "校园卡", "书籍资料", "生活用品", "衣物饰品", "其他物品"],
      
      // 失物列表数据
      lostItems: [
        {
          id: 1,
          title: "联想笔记本电脑",
          description: "蓝色外壳，在图书馆三楼靠窗位置遗失，电脑包上有个小熊挂件，内有重要学习资料",
          imageUrl: "/images/laptop.png",
          location: "图书馆三楼",
          publishTime: 1695789000000,
          category: "电子设备",
          type: "lost"
        },
        {
          id: 2,
          title: "校园卡",
          description: "姓名：王小明，学号：2022010101，捡到于一食堂一楼",
          imageUrl: "/images/card.png",
          location: "第一食堂",
          publishTime: 1695825000000,
          category: "校园卡",
          type: "found"
        },
        {
          id: 3,
          title: "《算法导论》教材",
          description: "图书馆借阅的教材，书内有笔记，在2号教学楼302教室遗失",
          imageUrl: "/images/book.png",
          location: "2号教学楼",
          publishTime: 1695699000000,
          category: "书籍资料",
          type: "lost"
        },
        {
          id: 4,
          title: "黑色保温杯",
          description: "膳魔师品牌，在研究生楼102教室捡到",
          imageUrl: "/images/cup.png",
          location: "研究生楼",
          publishTime: 1695612600000,
          category: "生活用品",
          type: "found"
        }
      ],
      
       // 点击帖子项时触发
    viewDetail(e) {
        // 获取当前点击帖子的id（从data-id中获取）
        const postId = e.currentTarget.dataset.id;
        
        // 跳转到帖子详情页，并携带id参数
        wx.navigateTo({
            url:`/pages/postDetail/postDetail?id=${postId}`
        });
    },

    // 你的其他函数（如formatTime）
    formatTime(time) {
        // 时间格式化逻辑
        return time;
    },

      // 底部导航
      currentTab: "home",
      noticeCount: 3,
      
      // 弹窗控制
      showPublishModal: false,
      
      // 加载更多控制
      showLoadMore: false
    },
    
    onLoad() {
        // 检查是否已登录
        const isLogin = wx.getStorageSync('isLogin');
        if (!isLogin) {
          // 未登录则跳转到登录页
          wx.redirectTo({
            url: '/pages/login/login'
          });
        }
      },
     
  // 点击发布失物按钮
  goToPublishLost() {
    wx.navigateTo({
      url: '/pages/publish/publish?type=lost'
    });
  },
  
  // 点击发布招领按钮
  goToPublishFound() {
    wx.navigateTo({
      url: '/pages/publish/publish?type=found'
    });
  },
    onLoad: function() {
      // 页面加载时获取数据
      this.loadLostItems();
      
      // 检查是否有未读通知
      this.checkUnreadNotices();
      
      // 检查是否需要显示公告
      const hideAnnouncement = wx.getStorageSync('hideAnnouncement');
      if (hideAnnouncement) {
        this.setData({ showAnnouncement: false });
      }
    },
    
    onPullDownRefresh: function() {
      // 下拉刷新
      setTimeout(() => {
        this.loadLostItems(true);
        wx.stopPullDownRefresh();
      }, 1000);
    },
    
    onReachBottom: function() {
      // 上拉加载更多
      if (this.data.lostItems.length < 20) { // 假设最多20条数据
        this.setData({ showLoadMore: true });
        setTimeout(() => {
          this.loadMoreItems();
          this.setData({ showLoadMore: false });
        }, 1500);
      }
    },
    
    // 关闭公告
    closeAnnouncement: function() {
      this.setData({ showAnnouncement: false });
      // 存储到本地，下次不再显示
      wx.setStorageSync('hideAnnouncement', true);
    },
    
    // 搜索输入
    onSearchInput: function(e) {
      this.setData({ searchValue: e.detail.value });
      // 实时搜索功能
      if (e.detail.value.length > 0) {
        this.searchItems(e.detail.value);
      } else {
        this.loadLostItems();
      }
    },
    
    
    // 切换标签
    switchTab: function(e) {
      const tab = parseInt(e.currentTarget.dataset.tab);
      this.setData({ activeTab: tab });
      // 根据标签筛选数据
      this.filterItemsByTab(tab);
    },
    
    // 切换底部导航
    switchBottomTab: function(e) {
      const tab = e.currentTarget.dataset.tab;
      this.setData({ currentTab: tab });
      // 实际项目中可以根据tab跳转到不同页面
      if (tab === 'discover') {
        wx.navigateTo({ url: '/pages/discover/discover' });
      } else if (tab === 'notice') {
        wx.navigateTo({ url: '/pages/notice/notice' });
      } else if (tab === 'mine') {
        wx.navigateTo({ url: '/pages/mine/mine' });
      }
    },
    
    // 查看详情
    viewDetail: function(e) {
      const id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: `/pages/postDetail/postDetail?id=${id}`
      });
    },
    
    // 查看全部
    // 查看全部
viewAll: function() {
    // 先保存当前活跃标签
    const currentTab = this.data.activeTab;
    // 跳转到列表页前先重新加载所有数据
    this.loadLostItems(true).then(() => {
      // 如果当前不是"全部"标签，需要先切换到全部标签并加载所有数据
      if (currentTab !== 0) {
        this.setData({ activeTab: 0 });
        this.filterItemsByTab(0);
      }
      // 跳转到列表页
      wx.navigateTo({
        url: `/pages/list/list?tab=${this.data.activeTab}`
      });
    });
  },
    
    // 显示发布选项
    showPublishOptions: function() {
      this.setData({ showPublishModal: true });
    },
    
    // 隐藏发布选项
    hidePublishOptions: function() {
      this.setData({ showPublishModal: false });
    },
    
    // 发布失物
    publishLost: function() {
      this.hidePublishOptions();
      wx.navigateTo({
        url: '/pages/publish/publish?type=lost'
      });
    },
    
    // 发布招领
    publishFound: function() {
      this.hidePublishOptions();
      wx.navigateTo({
        url: '/pages/publish/publish?type=found'
      });
    },
    
    // 我的记录
    myRecords: function() {
      wx.navigateTo({
        url: '/pages/myRecords/myRecords'
      });
    },
    
    // 设置
    setting: function() {
      wx.navigateTo({
        url: '/pages/setting/setting'
      });
    },
    
    // 格式化时间
    formatTime: function(timestamp) {
      const now = Date.now();
      const diff = now - timestamp;
      const minute = 60 * 1000;
      const hour = 60 * minute;
      const day = 24 * hour;
      const week = 7 * day;
      
      if (diff < minute) {
        return '刚刚';
      } else if (diff < hour) {
        return Math.floor(diff / minute) + '分钟前';
      } else if (diff < day) {
        return Math.floor(diff / hour) + '小时前';
      } else if (diff < week) {
        return Math.floor(diff / day) + '天前';
      } else {
        const date = new Date(timestamp);
        return date.getMonth() + 1 + '月' + date.getDate() + '日';
      }
    },
    
    // 加载失物数据
// 加载失物数据
loadLostItems: function(refresh = false) {
  return new Promise((resolve) => {
    // 实际项目中这里应该是网络请求
    if (refresh) {
      // 模拟刷新数据，恢复初始完整列表
      const initialItems = [
        {
          id: 1,
          title: "联想笔记本电脑",
          description: "蓝色外壳，在图书馆三楼靠窗位置遗失，电脑包上有个小熊挂件，内有重要学习资料",
          imageUrl: "/images/laptop.png",
          location: "图书馆三楼",
          publishTime: 1695789000000,
          category: "电子设备",
          type: "lost"
        },
        {
          id: 2,
          title: "校园卡",
          description: "姓名：王小明，学号：2022010101，捡到于一食堂一楼",
          imageUrl: "/images/card.png",
          location: "第一食堂",
          publishTime: 1695825000000,
          category: "校园卡",
          type: "found"
        },
        {
          id: 3,
          title: "《算法导论》教材",
          description: "图书馆借阅的教材，书内有笔记，在2号教学楼302教室遗失",
          imageUrl: "/images/book.png",
          location: "2号教学楼",
          publishTime: 1695699000000,
          category: "书籍资料",
          type: "lost"
        },
        {
          id: 4,
          title: "黑色保温杯",
          description: "膳魔师品牌，在研究生楼102教室捡到",
          imageUrl: "/images/cup.png",
          location: "研究生楼",
          publishTime: 1695612600000,
          category: "生活用品",
          type: "found"
        }
      ];
      this.setData({ lostItems: initialItems });
    }
    resolve();
  });
},
    
    // 加载更多
    loadMoreItems: function() {
      // 模拟加载更多数据
      const newItems = [
        {
          id: this.data.lostItems.length + 1,
          title: "篮球",
          description: "斯伯丁篮球，在篮球场捡到",
          imageUrl: "/images/basketball.png",
          location: "室外篮球场",
          publishTime: 1695526200000,
          category: "运动器材",
          type: "found"
        },
        {
          id: this.data.lostItems.length + 2,
          title: "机械手表",
          description: "黑色表带，在图书馆阅览室遗失",
          imageUrl: "/images/watch.png",
          location: "图书馆",
          publishTime: 1695439800000,
          category: "衣物饰品",
          type: "lost"
        }
      ];
      
      this.setData({
        lostItems: [...this.data.lostItems, ...newItems]
      });
    },
    
    // 根据标签筛选
    filterItemsByTab: function(tabIndex) {
      // 实际项目中这里应该从服务器请求对应分类的数据
      if (tabIndex === 0) {
        // 全部，加载所有数据
        this.loadLostItems();
      } else {
        // 筛选对应分类的数据
        const category = this.data.tabTitles[tabIndex];
        const filtered = this.data.lostItems.filter(item => item.category === category);
        this.setData({ lostItems: filtered });
      }
    },
    
    // 搜索物品
    searchItems: function(keyword) {
      const lowerKeyword = keyword.toLowerCase();
      const filtered = this.data.lostItems.filter(item => 
        item.title.toLowerCase().includes(lowerKeyword) || 
        item.description.toLowerCase().includes(lowerKeyword) ||
        item.location.toLowerCase().includes(lowerKeyword)
      );
      this.setData({ lostItems: filtered });
    },
    
    // 检查未读通知
    checkUnreadNotices: function() {
      // 实际项目中这里应该是请求服务器获取未读数量
      this.setData({ noticeCount: 3 });
    }
  });
      